local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local LanguageHelper=_G.LanguageHelper 
local PlayerInfoHelper=_G.ClientOHOH.load.PlayerInfoHelper()
local ConnectClass=_G.CommonOHOH.load.Connect();


function InitInsts(ui)
	ui.insts = {};
	ui.insts.close = "ctrl.close";
	ui.insts.cell = "ctrl.cell";
    ui.insts.OnBgColor = "ctrl.color.On";
    ui.insts.OffBgColor =  "ctrl.color.Off";

	ui.insts.setting={

        On={
            btn="ctrl.onBtn",
            -- lable="ctrl.Frame.switchOn.text",
        },

        Off={
            btn="ctrl.offBtn",
            -- lable="ctrl.Frame.switchOff.text",
        },

    }

end


function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.TradeSendMsgGui"));

    self.TradeSettingEvent=ConnectClass.new()--改变交易设置
    self.SendTradeEvent=ConnectClass.new()--发送交易请求

    self.insts.cell.Visible=false
    self.CellList={}

    CallBackManager:AddRbxConnect(self,self.insts.setting.On.btn.Activated, function()
            self.TradeSettingEvent:Action(false)
            self.insts.setting.On.btn.Visible=false
            self.insts.setting.Off.btn.Visible=true
	end);

    CallBackManager:AddRbxConnect(self,self.insts.setting.Off.btn.Activated, function()
        self.TradeSettingEvent:Action(true) 
        self.insts.setting.On.btn.Visible=false
        self.insts.setting.Off.btn.Visible=true
	end);

    CallBackManager:AddRbxConnect(self,self.insts.close.Activated, function()
            self:Hide()
	end);


end;


function M.dtor(self)
	M.super.dtor(self);
end;

function M.OnHide(self)




end

-- 0，关闭交易，1和所有人交易，2，仅和好友交易
function M.TradeSetting(self,isOpenTrade)
    self.insts.setting.On.btn.Visible=false
    self.insts.setting.Off.btn.Visible=false

    if isOpenTrade then
        self.insts.setting.On.btn.Visible=true
    else
        self.insts.setting.Off.btn.Visible=true
    end
end

function M.DestoryCellList(self)
    for _,v in pairs(self.CellList) do
        v:dtor()
    end
    self.CellList={}
end

function M.InitTradeCell(self,v)
    local obj=self.insts.cell:Clone()
    obj.Parent=self.insts.cell.Parent
    obj.Visible=true

    local pathStr={
        root="ctrl.root",
        headImage="ctrl.head",
        sendBtn="ctrl.btn",
        nickName="ctrl.nickName",
        sateText="ctrl.sendText",
        sateImage="ctrl.sendImg",
        btn="ctrl.btn",
        userName="ctrl.userName",

    }
    -- uiObj,pathStr
    local cellInst= self:InitUiInst2(obj,pathStr)

    cellInst.headImage.Image=v.head
    cellInst.userName.Text="@"..v.name
    cellInst.nickName.Text=v.nickName

    local cell={
        data=v,
        inst=cellInst,
        uiObj=obj,
        clickTime=os.clock(),

        isCdOver=function(this)
            if os.clock()-this.clickTime >0.5 then
                this.clickTime=os.clock()
                return true
            else
                return false
            end
        end,

        isCanTrade=function(this)
            local isTrade=true
            if  this.data.player.Trade.tradeSate.Value==1 then --交易中
                 isTrade=false
            end
            if isTrade and this.data.player.Trade.tradeSetting.Value==0 then
                isTrade=false
            end
            return isTrade
        end,

        updateSetting=function(this)
            this.inst.sateImage.Visible=false
            this.inst.sateText.Visible=false
            if this.data.player.Trade.tradeSetting.Value>0 then --启用
                this.inst.sateImage.Visible=true
                this.inst.root.BackgroundColor3=self.insts.OnBgColor
            else --禁用
                this.inst.root.BackgroundColor3=self.insts.OffBgColor
                this.inst.sateText.Visible=true
                this.inst.sateText.Text=LanguageHelper:GetText(134)
            end
        end,

        updateSate=function(this)
            this.inst.sateImage.Visible=false
            this.inst.sateText.Visible=false
            if this.data.player.Trade.tradeSate.Value==1 then--交易中
                this.inst.sateText.Visible=true
                this.inst.sateText.Text=LanguageHelper:GetText(30068)
            else --可以交易
                 this.inst.sateImage.Visible=true
            end
        end,

        
        update=function(this)
            -- print("----------update---------1-->")
            this:updateSetting() --更新设置，设置可以覆盖状态
            if this.data.player.Trade.tradeSetting.Value>0 then
                -- print("----------update--------2-->")
                this:updateSate()
            end
        end,

        init=function(this)
            --绑定事件
            this.data.player.Trade.tradeSetting.Changed:Connect(function()
                this:update()
            end)

            this.data.player.Trade.tradeSate.Changed:Connect(function()
                this:update()
            end)

            this.inst.btn.Activated:Connect(function()
                if this:isCdOver() then
                    if this:isCanTrade() then
                        self.SendTradeEvent:Action(this.data.player.userId)
                        --添加发送交易成功！
                        ClientOHOH.load.UIManager().ui_list.GlobalTipGui:Show(LanguageHelper:GetText(888005))
                    end
                else
                    ClientOHOH.load.UIManager().ui_list.GlobalTipGui:Show(LanguageHelper:GetText(30069))
                end
            end)

            this:update()
        end,

        dtor=function(this) 
            this.uiObj:Destroy()
        end
    }

    cell:init()
    self.CellList[v.userId]=cell


end

function M.AddPalyer(self,player)
    if self.CellList[player.UserId] then
        return
    end
    PlayerInfoHelper.GetPlayerInfo(player.UserId,function(h,n)
        self:InitTradeCell({
            ["player"]=player,
            userId=player.UserId,
            head=h,
            name=n,
            nickName=player.DisplayName
        })
    end)
end

function M.RemovePlayer(self,player)
    local userId=player.UserId
    local cell=self.CellList[userId]
    if cell then
        cell:dtor()
        self.CellList[userId]=nil
    end

end


return M
